﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BUS;
using DTO;
namespace CuaHangHoa
{
    public partial class Sua_ncc : Form
    {
        public Sua_ncc()
        {
            InitializeComponent();

        }

        public delegate void DoEvent();
        public event DoEvent RefreshDgv;

        private void CapNhatNCC()
        {
            Nhacungcap_DTO ncc = new Nhacungcap_DTO();

            ncc.MaNCC = int.Parse(txt_mancc.Text);
            ncc.TenNCC = txt_tenncc.Text;
            ncc.SDT = int.Parse(txt_SDT.Text);
            ncc.Email = txt_Email.Text;
            ncc.Diachi = txt_diachi.Text;
            ncc.Fax = txt_Fax.Text;
            ncc.Website = txt_Website.Text;
            ncc.Taikhoan = txt_TKNH.Text;
            ncc.TinhTrang = txt_tinhtrang.Text;
            ncc.GhiChu = txt_ghichu.Text;

            Nhacungcap_BUS bus = new Nhacungcap_BUS();
            int kq = bus.CapNhatNhaCungCap(ncc);

            if (kq == 1)
            {
                this.RefreshDgv();
                MessageBox.Show("Sửa thành công");
            }
            else
            {
                MessageBox.Show("Sửa thất bại");
            }
            txt_tenncc.Focus();
        }

        public void Loaddata_gv(Nhacungcap_DTO ncc)
        {
            txt_mancc.Text = ncc.MaNCC.ToString();
            txt_tenncc.Text = ncc.TenNCC;
            txt_SDT.Text = ncc.SDT.ToString();
            txt_Email.Text = ncc.Email;
            txt_diachi.Text = ncc.Diachi;
            txt_Fax.Text = ncc.Fax;
            txt_Website.Text = ncc.Website;
            txt_TKNH.Text = ncc.Taikhoan;
            txt_tinhtrang.Text = ncc.TinhTrang;
            txt_ghichu.Text = ncc.GhiChu;
        }

        private void btCapnhat_Click_1(object sender, EventArgs e)
        {
            CapNhatNCC();
            errorProvider1.Clear();
        }

        private void btThoat_Click_1(object sender, EventArgs e)
        {
            Close();
        }

        private void txt_tenncc_KeyDown_1(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                txt_SDT.Focus();
            }
        }

        private void txt_diachi_KeyDown_1(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                txt_Fax.Focus();
            }
        }

        private void txt_SDT_KeyDown_1(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                txt_Email.Focus();
            }
        }

        private void txt_Website_KeyDown_1(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                txt_TKNH.Focus();
            }
        }

        private void txt_Email_KeyDown_1(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                txt_diachi.Focus();
            }
        }
        private void txt_Fax_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                txt_Website.Focus();
            }
        }

        private void txt_tinhtrang_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                txt_ghichu.Focus();
            }
        }
        private void txt_TKNH_KeyDown_1(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                txt_tinhtrang.Focus();
            }
        }

        private void textBox4_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                CapNhatNCC();
                errorProvider1.Clear();
            }
        }

        private void txt_SDT_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
                errorProvider1.SetError(txt_SDT, "Chi được nhập số");
            }
        }

        private void Sua_ncc_FormClosing(object sender, FormClosingEventArgs e)
        {
            DialogResult ok;
            ok = MessageBox.Show("Bạn có muốn thoát ?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (ok == DialogResult.No)
            {
                e.Cancel = true;
            }
        }

        

       
    }
}
